using B.S.XZYData.Api.Read.Application.Command.Plan;
using B.S.XZYData.Api.Read.Dto.Plan;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 生产计划读操作控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class PlanController : ControllerBase
    {
        private readonly IMediator _mediator;

        public PlanController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取生产计划分页列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="planCode">计划单号（模糊查询）</param>
        /// <param name="remark">备注（模糊查询）</param>
        /// <param name="createBy">创建人</param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ApiPageing<PlanDto>> GetPlanPage(
            [FromQuery] int index = 1,
            [FromQuery] int size = 20,
            [FromQuery] string? planCode = null,
            [FromQuery] string? remark = null,
            [FromQuery] string? createBy = null)
        {
            var command = new GetPlanPageCommand
            {
                Index = index,
                Size = size,
                PlanCode = planCode,
                Remark = remark,
                CreateBy = createBy
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取生产计划详情（包含明细列表）
        /// </summary>
        /// <param name="id">生产计划ID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<PlanDetailDto>> GetPlanDetail(int id)
        {
            var command = new GetPlanDetailCommand
            {
                PlanId = id
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取生产计划明细列表
        /// </summary>
        /// <param name="id">生产计划ID</param>
        /// <returns></returns>
        [HttpGet("{id}/items")]
        public async Task<ApiResult<List<PlanItemDto>>> GetPlanItems(int id)
        {
            var command = new GetPlanItemsCommand
            {
                PlanId = id
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取产品选择列表（用于添加计划明细时选择产品）
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="productCode">产品编号（模糊查询）</param>
        /// <param name="productName">产品名称（模糊查询）</param>
        /// <returns></returns>
        [HttpGet("product-select")]
        public async Task<ApiPageing<ProductSelectDto>> GetProductSelect(
            [FromQuery] int index = 1,
            [FromQuery] int size = 20,
            [FromQuery] string? productCode = null,
            [FromQuery] string? productName = null)
        {
            var command = new GetProductSelectCommand
            {
                Index = index,
                Size = size,
                ProductCode = productCode,
                ProductName = productName
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取所有生产计划明细分页列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="productCode">产品编号（模糊查询）</param>
        /// <param name="productName">产品名称（模糊查询）</param>
        /// <param name="workOrderCode">工单编号（模糊查询）</param>
        /// <param name="planId">计划ID（精确查询）</param>
        /// <returns></returns>
        [HttpGet("items/all")]
        public async Task<ApiPageing<PlanItemDto>> GetAllPlanItems(
            [FromQuery] int index = 1,
            [FromQuery] int size = 20,
            [FromQuery] string? productCode = null,
            [FromQuery] string? productName = null,
            [FromQuery] string? workOrderCode = null,
            [FromQuery] int? planId = null)
        {
            var command = new GetAllPlanItemsCommand
            {
                Index = index,
                Size = size,
                ProductCode = productCode,
                ProductName = productName,
                WorkOrderCode = workOrderCode,
                PlanId = planId
            };

            return await _mediator.Send(command);
        }
    }
} 